<template>
	<view class="container">
    <view class="cu-card case" :class="isCard?'no-card':''">
      <view class="cu-item shadow" style="height: 160px">
        <view class="image">
          <image :src="titleURL"
                 mode="widthFix" style="height: 200px"></image>
          <view v-if="agentData.mobile!=undefined" class="cu-tag text-xxl" style="background: transparent; right: 5px;bottom: 10px;top: auto;" @click="naviTo('/pages/tabbar/tabbar-2/joinList')">
            <view class="cuIcon-peoplefill lines-yellow "></view>
          </view>
        </view>

      </view>
    </view>
    <view class="bg-white padding">
      <view class="grid col-4 grid-square">
        <view v-for="(item,index) in tableList" :key="index" style="height: 80px;text-align: center" @click="swichPage(item)">
          <view style="display: inline-block">

            <view  class="bg-img" style="width:50px;height: 50px;border-radius: 6px;" :style="[{ backgroundImage:'url(' + item.themb+ ')' }]"></view>
          </view>
          <view>{{item.name}}</view>
        </view>
      </view>
    </view>
<!--    <view class=" padding">-->
<!--      <view class="flex">-->
<!--        <view class="flex-sub cu-btn margin-lr-sm bg-gradual-blue " @click="goPage(0)">学历提升</view>-->
<!--        <view class="flex-sub cu-btn margin-lr-sm bg-gradual-blue" @click="goPage(1)">技能培训</view>-->
<!--        <view class="flex-sub cu-btn margin-lr-sm bg-gradual-blue" @click="goPage(2)">职称评审</view>-->
<!--      </view>-->
<!--    </view>-->

		<!-- 绑定 -->
		<goReg
			ref="goReg"
			:contentHeight="580"
		></goReg>
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	import empty from "@/components/empty";

	export default {
		components: {
			uniLoadMore,
			empty
		},
		data() {
			return {
				tabCurrentIndex: 0,
        avatar:[1,2,3,4,5,6,],
        titleURL:this.$APP_URL+'files/title.png',
				navList: [
				  {
						state: 0,
						text: '求职指导',
						value:1,
						loadingType: 'more',
						cateList: []
					},
					{
						state: 1,
						text: '职场培训',
						value:2,
						loadingType: 'more',
						cateList: []
					},
					{
						state: 2,
						text: '劳动法规',
						value:3,
						loadingType: 'more',
						cateList: []
					},
					// {
					// 	state: 3,
					// 	text: '已购课程',
					// 	value:4,
					// 	loadingType: 'more',
					// 	cateList: []
					// },
				],
				modalName: null,
				cateIndex:-1,
				catePicker:[],
        agentData:[],
				cateIds:[],
				cateId:0,
				typeId:0,
				page:1,
				uId:0,
				kwd:'',
				bg_img:'https://apitest.hrzhizhang.com/file/2020-11-27/qzbg.jpg',
				tableList:[],
			};
		},
    onShow(){
      let t = this;
      t.uId = uni.getStorageSync('userId');
      let uId = t.uId

      if(uId>"0"&&uId!=undefined){
        this.getUserInfo();

      }
    },
		onLoad(query){
			/**
			 * 修复app端点击除全部订单外的按钮进入时不加载数据的问题
			 * 替换onLoad下代码即可
			 */
			console.log(query);
			const scene = decodeURIComponent(query.scene)
			console.log(scene);

      this.uId = uni.getStorageSync('userId');

			this.getCates();

		},

		methods: {
      async getUserInfo(){
        console.log('in getUserInfo')
        let uId = this.uId;
        let data = await this.$http.get(
          'wx/job_seekers/'+uId,
        );
        console.log(data);
        this.userInfo = data;
        this.agentData = data.agent
        uni.setStorageSync('userInfo',data);

        uni.setStorageSync('userRoles',data.roles);
        // this.$forceUpdate()
      },
      naviTo(page){
        let t = this
        t.uId = uni.getStorageSync('userId');
        let uId = t.uId

        if(uId>"0"&&uId!=undefined){
          uni.navigateTo({
            url:page
          });
        }
        else{
          t.$refs.goReg.showModal('regModal');
        }
      },
      goPage(page) {
        // let uId = uni.getStorageSync('userId');
        // if(page==''){
        //   uni.showToast({
        //     title:'敬请期待...',
        //     icon:'none',
        //   })
        //   return
        // }

        // console.log(page);
        // return;
        let t = this
        t.uId = uni.getStorageSync('userId');
        let uId = t.uId

        if(uId>"0"&&uId!=undefined){
          uni.navigateTo({
            url: '/pages/tabbar/company/image?index='+page
          });
        }
        else{
          t.$refs.goReg.showModal('regModal');
        }

      },
      swichPage(item) {
        // let uId = uni.getStorageSync('userId');
        // uni.showToast({
        //   title:'敬请期待...',
        //   icon:'none',
        // })
        console.log(item);
        uni.setStorageSync('pType',item);
        // return;
        uni.switchTab({
          url: '/pages/tabbar/tabbar-2/socialSecurity'
        });
      },
			//获取订单列表
			async loadData(type='add'){
				//这里是将订单挂载到tab列表下
				let index = this.tabCurrentIndex;

				let navItem = this.navList[index];
				let state = navItem.state;

				// if(navItem.loadingType === 'loading'){
				// 	//防止重复加载
				// 	return;
				// }
				//没有更多直接返回
				if(type === 'add'){
					if(navItem.loadingType === 'noMore'){
						return;
					}

				}

				navItem.loadingType = 'loading';
				if(this.tabCurrentIndex==3){
					let param = {
						kwd:this.kwd,
						user_id:this.uId,
						page:this.page,
						order_type:1,
						limit:5,
						};
					let that = this;
					let data = await this.$http.post(
						'trainOrderList', //可以直接放链接(将不启用全局定义域名)
						param,
					);
					console.log(data);
					// if(data.last_page<=that.page){
					// 	navItem.loadingType = 'noMore';
					// }
					// else{
					// 	navItem.loadingType = 'more';
					// }
					// data.data.forEach(item => {
					// 	item.updated_at = ;
					// })
					that.tableList=that.tableList.concat(data.data)
					// that.tableList.push(data.data);
					that.$set(navItem, 'loaded', true);
					that.page++;
					// setTimeout(()=>{

					// 	this.$set(navItem, 'loaded', true);

					// 	//判断是否还有数据， 有改为 more， 没有改为noMore
					// 	navItem.loadingType = 'more';
					// }, 600);
				}
				else{
					let param = {
						kwd:this.kwd,
						user_id:this.uId,
						type:this.typeId,
						cate_id:this.cateId,
						page:this.page,
						status:2,
						limit:5,
						};
					let that = this;
					let data = await this.$http.post(
						'trainList', //可以直接放链接(将不启用全局定义域名)
						param,
					);
					console.log(data);
					// if(data.last_page<=that.page){
					// 	navItem.loadingType = 'noMore';
					// }
					// else{
					// 	navItem.loadingType = 'more';
					// }
					that.tableList=that.tableList.concat(data.data)
					// that.tableList.push(data.data);
					that.$set(navItem, 'loaded', true);
					that.page++;
					// setTimeout(()=>{

					// 	this.$set(navItem, 'loaded', true);

					// 	//判断是否还有数据， 有改为 more， 没有改为noMore
					// 	navItem.loadingType = 'more';
					// }, 600);
				}


			},
			async getOpenId(code){
				let param = {
					wx_code:code,
					};
				let data = await this.$http.post(
					'openId', //可以直接放链接(将不启用全局定义域名)
					param,
				);
				this.uId = data.id;
				uni.setStorageSync('userId',data.id);
				uni.setStorageSync('userInfo',data);
				this.getCates();
			},
			goDetail(item){
				let uId = uni.getStorageSync('userId');
				// let uId =1;
				if(uId>"0"&&uId!=undefined){
					uni.setStorageSync('trainInfo',item);
					uni.navigateTo({
						// url: `/pages/works/detail?id=${id}`
						url: `/pages/tabbar/tabbar-1/trainInfo`
					})
					return;
				}

				this.$refs.goReg.showModal('regModal');
				return;

			},
			onSearch(){
				this.typeId=0;
				this.cateId=0;
				this.page=1;
				this.tableList=[];
				this.loadData('refush');
			},
			//swiper 切换
			changeTab(e){
				this.tabCurrentIndex = e.target.current;
				this.loadData('tabChange');
			},
			//顶部tab点击
			tabClick(index){
				if(index==1){
					this.bg_img='https://apitest.hrzhizhang.com/file/2020-11-27/pybg.jpg'
				}
				else if(index==2){
					this.bg_img='https://apitest.hrzhizhang.com/file/2020-11-27/ldbg.jpg'
				}
				else{
					this.bg_img='https://apitest.hrzhizhang.com/file/2020-11-27/qzbg.jpg'
				}
				this.tabCurrentIndex = index;
				console.log(index)
				this.catePicker=['全部类型'];
				this.cateIds=[-1];

				let cate = this.navList[index].cateList;
				cate.forEach(item => {
					this.catePicker.push(item.label);
					this.cateIds.push(item.value);
				})
				this.typeId = this.navList[index].value;

				this.cateId = 0;
				this.cateIndex = -1;
				this.page=1;
				this.tableList=[];
				this.loadData('refush');
			},
			cateChange(e){
				let index = e.detail.value
				this.cateIndex = index;
				let cateId = this.cateIds[index];

				console.log(cateId)
				this.cateId = cateId>0?cateId:0;
				this.page=1;
				this.tableList=[];
				this.loadData('refush');
			},
			async getCates(){
				let param = '';
				let that = this;
				let data = await this.$http.get(
					'wx/job_cate', //可以直接放链接(将不启用全局定义域名)
					param,
				);
        this.tableList = data;
			},
		},
	}
</script>

<style lang="scss">
	page, .container{
		background: #F6F6F6;
		height: 100%;
	}

	.container{
		.box{
			position: fixed !important;
			top:0px;
			width: 100%;
		}
	}

	.cu-card>.cu-item{
		padding: 0 !important;
    border-radius: 0;
		background-color: #F2F6F8 !important;
		/*margin-bottom: 15px !important;*/
    margin: 0 !important;
	}

</style>
